package com.dxx.cloud.admin.controller.admin;

import cn.hutool.core.lang.Assert;
import com.dxx.cloud.admin.module.entity.GoodsEntity;
import com.dxx.cloud.admin.module.entity.GoodsSizeEntity;
import com.dxx.cloud.admin.module.request.*;
import com.dxx.cloud.admin.module.response.GoodsResponse;
import com.dxx.cloud.admin.module.response.GoodsSizeResponse;
import com.dxx.cloud.admin.service.IGoodsService;
import com.dxx.cloud.admin.service.IGoodsSizeService;
import com.dxx.cloud.admin.service.IGoodsTypeService;
import com.dxx.cloud.admin.utils.BaseControllerUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goods")
@Tag(name = "商品模块")
@Slf4j
public class GoodsController {

    @Autowired
    private IGoodsTypeService goodsTypeService;

    @Autowired
    private IGoodsSizeService goodsSizeService;

    @Autowired
    private IGoodsService goodsService;


    @Operation(summary = "添加商品类别")
    @RequestMapping(value = "/addType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R addType(@RequestBody @Validated GoodsTypeAddRequest request) throws Exception {
        request.setCreator(request.getOperator());
        return R.ok(goodsTypeService.save(request));
    }

    @Operation(summary = "商品类别列表")
    @RequestMapping(value = "/typeList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R typeList(@RequestBody @Validated GoodsTypeListRequest request) throws Exception {
        log.info("只能获取默认的商品类型");
        return R.ok(goodsTypeService.listByRequest(request));
    }

    @Operation(summary = "修改一级商品类别信息")
    @RequestMapping(value = "/updateTypeById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R updateTypeById(@RequestBody @Validated GoodsTypeUpdateRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        return BaseControllerUtils.updateById(goodsTypeService.getBaseMapper(),request.getId(),request);
    }

    @Operation(summary = "删除类别")
    @RequestMapping(value = "/removeType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R updateTypeById(@RequestBody @Validated IdRequest request) throws Exception {
        return R.ok(goodsTypeService.removeById(request.getId()));
    }

    @Operation(summary = "查询商品列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<List<GoodsResponse>> list(@RequestBody @Validated GoodsListRequest request) throws Exception {
        List<GoodsResponse> responseList = goodsService.goodsList(request);
        return R.ok(responseList);
    }

    @Operation(summary = "查询商品列表")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public TableDataInfo<GoodsResponse> pageList(@RequestBody @Validated GoodsPageListRequest request) throws Exception {
        return goodsService.pageList(request);
    }



    @Operation(summary = "添加商品")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Transactional
    public R<Long> add(@RequestBody @Validated GoodsAddRequest request) throws Exception {
        return R.ok(goodsService.addGoods(request));
    }

    @Operation(summary = "查询商品")
    @RequestMapping(value = "/detailById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<GoodsResponse> detailById(@RequestBody @Validated IdRequest request)  {
        return R.ok(goodsService.detailResponseById(request.getId()));
    }

    @Operation(summary = "删除商品")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Transactional
    public R<Boolean> deleteById(@RequestBody @Validated IdRequest request) throws Exception {
        return R.ok(goodsService.removeById(request.getId()));
    }

    @Operation(summary = "修改商品信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R updateById(@RequestBody @Validated GoodsUpdateRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        return BaseControllerUtils.updateById(goodsService.getBaseMapper(),request.getId(),request);
    }

    @Operation(summary = "查询商品规格列表")
    @RequestMapping(value = "/sizeList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<List<GoodsSizeResponse>> sizeList(@RequestBody @Validated GoodsSizeListRequest request) throws Exception{
        GoodsEntity entity = goodsService.getById(String.valueOf(request.getGoodsId()));
        List<GoodsSizeResponse> list = goodsSizeService.goodsSizeResponseList(request);
        for (GoodsSizeResponse goodsSizeResponse : list) {
            goodsSizeResponse.setName(entity.getName());
        }
        return R.ok(list);
    }

    @Operation(summary = "添加商品规格")
    @RequestMapping(value = "/addSize", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Transactional
    public R<Long> add(@RequestBody @Validated GoodsSizeAddRequest request) throws Exception {
        return R.ok(goodsSizeService.addSize(request));
    }

    @Operation(summary = "编辑商品规格")
    @RequestMapping(value = "/updateSizeBySizeId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Transactional
    public R<Boolean> updateSizeBySizeId(@RequestBody @Validated GoodsSizeEntity request) throws Exception {
        Assert.notNull(request.getId(),"商品规格id不能为空");
        return R.ok(goodsSizeService.updateById(request));
    }

    @Operation(summary = "编辑商品规格")
    @RequestMapping(value = "/deleteSizeById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @Transactional
    public R<Boolean> deleteSizeById(@RequestBody @Validated IdRequest request) throws Exception {
        Assert.notNull(request.getId(),"商品规格id不能为空");
        return R.ok(goodsSizeService.removeById(request.getId()));
    }


}
